package edu.fjut.se1603.tata.util;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author admin
 */
public class ConnectMySQL {
    public static  Connection con=null;
    private static final String URL="jdbc:mysql://localhost:3306/shangdian?zeroDateTimeBehavior=convertToNull";
    public static final String USER_NAME="root";
     public static final String PASSWORD="root";
    
    
   public static Connection Connect()
    {
     
      try{
          //1 加载驱动
          Class.forName("com.mysql.jdbc.Driver");
         con=DriverManager.getConnection(URL,USER_NAME, PASSWORD);
       /*  //3执行查询
          Statement st = con.createStatement();
          ResultSet rs = st.executeQuery("select * from pet"); //显示pet表中的信息
          //4处理集
          while(rs.next())
          {
              int id=rs.getInt(1);
              String Pet_Name=rs.getString("NAME");
              System.out.println(id+Pet_Name);
              
          }*/
      }
        catch (ClassNotFoundException ex) {
            Logger.getLogger(ConnectMySQL.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ConnectMySQL.class.getName()).log(Level.SEVERE, null, ex);
        }  
    /*  finally{
        //5关闭连接
        if(con!=null)
        {
            try {
                con.close();
            } catch (SQLException ex) {
                Logger.getLogger(ConnectMySQL.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
      }*/
        return con;
    }
   
    public void conclose()
    {
         if(con!=null)
        {
            try {
                con.close();
            } catch (SQLException ex) {
                Logger.getLogger(ConnectMySQL.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
